from pyflink.datastream import StreamExecutionEnvironment, MapFunction

#创建环境
env = StreamExecutionEnvironment.get_execution_environment()

env.set_parallelism(1)

#读取数据
student_ds = env.read_text_file('../../data/students.txt')

#1、使用lambda表达式
clazz_ds = student_ds.map(lambda line: line.split(",")[4])
# clazz_ds.print()


#2、定义函数的方式

def map_func(line):
    split = line.split(',')
    age = int(split[2])
    return age


age_ds = student_ds.map(map_func)
# age_ds.print()

#定义类的方式
#MyFunction:多了open和close方法，可以用于初始化操作，比如创建数据库连接
class MyFunction(MapFunction):

    #实现父类的抽象方法
    def map(self, line):
        split = line.split(',')
        sex = split[3]
        return sex


sex_ds = student_ds.map(MyFunction())

sex_ds.print()

env.execute()